package com.mib.browser;

import com.ireasoning.util.MibTreeNode;

import javax.swing.*;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.*;
import java.awt.*;

public class MIBBrowserTree extends JTree {
    protected DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("MIB文件");
    protected DefaultTreeModel treeModel;

    public MIBBrowserTree() {
        this.treeModel = new DefaultTreeModel(this.rootNode);
        this.treeModel.addTreeModelListener(new MIBBrowserTreeModelListener());
        this.setModel(this.treeModel);
        this.getSelectionModel().setSelectionMode(1);
        this.setShowsRootHandles(true);
        this.setCellRenderer(new MyRenderer());
    }

    public void clear() {
        this.rootNode.removeAllChildren();
        this.treeModel.reload();
    }

    public void removeCurrentNode() {
        TreePath currentSelection = this.getSelectionPath();
        if (currentSelection != null) {
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)(currentSelection.getLastPathComponent());
            MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
            if (parent != null) {
                this.treeModel.removeNodeFromParent(currentNode);
            }
        }

    }

    public DefaultMutableTreeNode openMIBFile(MibTreeNode childNode, boolean shouldBeVisible) {
        DefaultMutableTreeNode node = this.convertToMIBBrowserTreeNode(childNode, null);
        this.treeModel.insertNodeInto(node, this.rootNode, this.rootNode.getChildCount());
        if (shouldBeVisible) {
            this.scrollPathToVisible(new TreePath(node.getPath()));
        }

        return node;
    }

    public DefaultMutableTreeNode convertToMIBBrowserTreeNode(MibTreeNode mibNode, DefaultMutableTreeNode node) {
        if (mibNode == null) {
            return null;
        } else {
            if (node == null) {
                String rep = (String)mibNode.getName();
                MibTreeNode n = mibNode;

                for(int i = 0; i < 6; ++i) {
                    n = (MibTreeNode)n.getFirstChild();
                }

                node = new DefaultMutableTreeNode(new MIBBrowserTreeNode(n, rep, true));
                mibNode = n;
            }

            for(MibTreeNode n = (MibTreeNode)mibNode.getFirstChild(); n != null; n = (MibTreeNode)n.getNextSibling()) {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(new MIBBrowserTreeNode(n, (String)n.getName(), false));
                node.add(child);
                this.convertToMIBBrowserTreeNode(n, child);
            }

            return node;
        }
    }

    public void unloadSelectedFile() {
        TreePath path = this.getSelectionPath();
        if (path.getPathCount() >= 2) {
            this.treeModel.removeNodeFromParent((DefaultMutableTreeNode)path.getPathComponent(1));
            this.treeModel.reload();
        }
    }

    class MyRenderer extends DefaultTreeCellRenderer {
        ImageIcon leafIcon;
        ImageIcon tableIcon;

        public MyRenderer() {
            Image img = Toolkit.getDefaultToolkit().getImage("res/leaf.gif");
            this.leafIcon = new ImageIcon(img);
            img = Toolkit.getDefaultToolkit().getImage("res/table.gif");
            this.tableIcon = new ImageIcon(img);
        }

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            if (this.isTableNode(value)) {
                this.setIcon(this.tableIcon);
                this.setToolTipText(null);
            } else if (leaf) {
                this.setIcon(this.leafIcon);
                this.setToolTipText(null);
            }

            return this;
        }

        protected boolean isTableNode(Object value) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
            if (!(node.getUserObject() instanceof MIBBrowserTreeNode)) {
                return false;
            } else {
                MIBBrowserTreeNode n = (MIBBrowserTreeNode)node.getUserObject();
                MibTreeNode mib = n.getMibTreeNode();
                return mib.isTableNode();
            }
        }
    }

    class MIBBrowserTreeNode {
        private MibTreeNode mibTreeNode;
        private String nodeString;
        private boolean isFileNode;

        public MIBBrowserTreeNode(MibTreeNode node, String nodeString, boolean isFileNode) {
            this.mibTreeNode = node;
            this.nodeString = nodeString;
            this.isFileNode = isFileNode;
        }

        public MibTreeNode getMibTreeNode() {
            return this.mibTreeNode;
        }

        public String toString() {
            return this.nodeString;
        }

        public boolean isFileNode() {
            return this.isFileNode;
        }
    }

    class MIBBrowserTreeModelListener implements TreeModelListener {
        MIBBrowserTreeModelListener() {
        }

        public void treeNodesChanged(TreeModelEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent());

            try {
                int index = e.getChildIndices()[0];
                node = (DefaultMutableTreeNode)(node.getChildAt(index));
            } catch (NullPointerException var4) {
            }

            System.out.println("The user has finished editing the node.");
            System.out.println("New value: " + node.getUserObject());
        }

        public void treeNodesInserted(TreeModelEvent e) {
        }

        public void treeNodesRemoved(TreeModelEvent e) {
        }

        public void treeStructureChanged(TreeModelEvent e) {
        }
    }
}
